<?php
/**
 * This class is used to append task to HTTP Simple Queue Service,
 * Which is a Message Queue. And the task will be processed by a
 * PHP client.
 *
 * @author jszhang
 */
class TaskManager {
    /**
     * php client for HTTP Simple Queue Service
     * @var httpsqs
     */
    protected $m_httpsqs;

    protected $m_host = "localhost";
    protected $m_port = 1218;
    protected $m_charset = 'utf-8';

    /**
     * tag, paymethod, feature update task
     * send mail task
     * user reputation update task
     * shop score update task
     *
     * @var array
     */
    protected $m_tasktype = array(
            "SHOP_INFO_STATISITC" => "SHOP_INFO_STATISITC",
            "MAIL_SEND" => "MAIL_SEND",
            "USER_REPUTATION" => "USER_REPUTATION",
            "SHOP_SCORE" => "SHOP_SCORE",
            "PRODUCT_SCORE" => "PRODUCT_SCORE"
    );

    public function TaskManager() {
        $this->m_httpsqs = new httpsqs;
    }

    /**
     * data contains shop_id, tag, paymethod, feature
     * and will be composed to the following format:
     *
     * shop_id|tagname tagname tagname|paymethodid,paymethodid,paymethodid|featureid,featureid,featureid
     *
     * @param array $data
     */
    public function AddShopInfoTask( $data ) {
        $strdata = $data["shop_id"]."|";
        $strdata .= ($data["tag"]==""?" ":$data["tag"])."|";
        $strdata .= ($data["paymethod"]==""?" ":$data["paymethod"])."|";
        $strdata .= $data["feature"]==""?" ":$data["feature"];
        $this->m_httpsqs->pput( $this->m_host,
                $this->m_port,
                $this->m_charset,
                $this->m_tasktype["SHOP_INFO_STATISITC"],
                $strdata );
    }

    /**
     * only record shop id here
     * to let backend know which shop's score should be recalculated
     *
     * @param int $data
     */
    public function AddShopScoreTask( $data ) {
        $this->m_httpsqs->pput( $this->m_host,
                $this->m_port,
                $this->m_charset,
                $this->m_tasktype["SHOP_SCORE"],
                $data );
    }

    public function AddProductScoreTask( $data ) {
        $this->m_httpsqs->pput( $this->m_host,
                $this->m_port,
                $this->m_charset,
                $this->m_tasktype["PRODUCT_SCORE"],
                $data );
    }
}
?>
